<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="$emit('close', false)"
  >
    <!-- 这里准备复选框 -->
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="idlist">
      <!-- 选项 -->
      <el-checkbox :label="item.id" v-for="item in list" :key="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click.native="makesure"
        >确定</el-button
      >
      <el-button size="small" @click.native="$emit('close', false)"
        >取消</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { assignRoles } from '@/api/employess'
export default {
  // 组件名称
  name: 'assignrole',
  // 组件参数 接收来自父组件的数据
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default () {
        return ''
      }
    }
  },
  // 局部注册的组件
  components: {},
  // 组件状态值
  data () {
    return {
      idlist: [],
      list: [],
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 100,
        total: 0 // 记录总数
      }
    }
  },
  methods: {
    getRoleLists () {
      getRoleList(this.page).then(res => {
        this.list = res.data.rows
      })
    },
    makesure () {
      this.$confirm('确定分配角色吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除部门网络请求
        assignRoles({ id: this.id, roleIds: this.idlist }).then(res => {
          this.$message.success('分配角色成功')
          this.$emit('close', false)
          // 更新视图
        })
      }).catch(() => { });
    }
  },
  created () {
    this.getRoleLists()
  }
}
</script>
<style scoped lang="scss">
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>